(function() {

    var fill25 = function(tr, m) {
        if (!m) return;
        m.reverse();
        tr.empty();
        for (var i = 0; i < 25; i++) {
            var index = i;
            if (m.length < 25) {
                index = i + (m.length - 25);
            }
            if (index < 0) tr.append("<td> </td>");
            else {
                var c = "";
                for (var j = 0 ; j < m[index].c; j++) {
                    if (j == 0) c = c + m[index].n;
                    else c += "<br/>" + m[index].n;
                }
                tr.append("<td>" + c + "</td>");
            }
        }
    };

    var fillCount = function(tr, m, total, from0) {
        var qiu = parseInt($("#stat_qiu th.select").attr("qiu"));
        var key = $("#stat_type th.select").attr("type");
        $("#q-title").html($("#stat_qiu th.select").html());

        tr.empty();
        for (var i = 0; i < total; i++) {
            var t = (m[i] || 0);
            if (!from0) t = parseInt(t) + 1;
            tr.append("<td>" + t + "</td>");
        }
    }

    var kl8F = function(list) {
        var qiu = 1;
        var key = $("#stat_type th.select").attr("type");
    
        // 25列
        var m = gameRule.luzhu(list, qiu, key);
        fill25($("#qiu_nums"), m);
    }

    var eleven5F = function(list) {
        var qiu = parseInt($("#stat_qiu th.select").attr("qiu"));
        var key = $("#stat_type th.select").attr("type");
    
        // 填充count
        var m = gameRule.luzhu(list, qiu, "count");
        fillCount($("#qiu_body"), m, 11);

        // 25列
        var m = gameRule.luzhu(list, qiu, key);
        fill25($("#qiu_nums"), m, 11, false);
    }

    var pk10F = function(list) {
        var qiu = parseInt($("#stat_qiu th.select").attr("qiu"));
        var key = $("#stat_type th.select").attr("type");
    
        // 填充count
        var m = gameRule.luzhu(list, qiu, "count");
        fillCount($("#qiu_body"), m);
    
        // 25列
        var m = gameRule.luzhu(list, qiu, key);
        fill25($("#qiu_nums"), m, 10, false);
    }

    var sscF = function(list) {
        var qiu = parseInt($("#stat_qiu th.select").attr("qiu"));
        var key = $("#stat_type th.select").attr("type");
    
        // 填充count
        var m = gameRule.luzhu(list, qiu, "count");
        fillCount($("#qiu_body"), m);
        
        // 25列
        var m = gameRule.luzhu(list, qiu, key);
        fill25($("#qiu_nums"), m, 10, true);
    }

    var tenF = function (list) {
        var qiu = parseInt($("#stat_qiu th.select").attr("qiu"));
        var key = $("#stat_type th.select").attr("type");
    
        // 填充count
        var m = gameRule.luzhu(list, qiu, "count");
        fillCount($("#qiu_body"), m, 20);
    
        // 25列
        var m = gameRule.luzhu(list, qiu, key);
        fill25($("#qiu_nums"), m, 20, false);
    }

    window.initLuZhu = function(game) {
        if (!context.result || !context.result[game]) return;
        var list = context.result[game];

        var f1 = function(drawF) {
            $("#stat_qiu th").click(function(e) {
                var th = $(e.currentTarget);
                if (th.attr("class").indexOf("select") != -1) return;
                $("#stat_qiu th").removeClass("select");
                th.addClass("select");
            
                drawF(context.result[game]);
            });
        };
        var f2 = function(drawF) {
            $("#stat_type th").click(function(e) {
                var th = $(e.currentTarget);
                if (th.attr("class").indexOf("select") != -1) return;
                $("#stat_type th").removeClass("select");
                th.addClass("select");
            
                drawF(context.result[game]);
            });
        };

        switch (game) {
            case "bjkl8":
            case "zykl8": 
                f2(kl8F);
                kl8F(list); return;

            case "gd115":
            case "zy115": 
                f1(eleven5F);
                f2(eleven5F);
                eleven5F(list); return;

            case "bjpk10":
            case "zypk10": 
                f1(pk10F);
                f2(pk10F);
                pk10F(list); return;

            case "cqssc":
            case "tjssc":
            case "xjssc":
            case "zyssc":
            case "ffcqq": 
                f1(sscF);
                f2(sscF);
                sscF(list); return;

            case "cqkl10":
            case "gdkl10":
            case "zykl10": 
                f1(tenF);
                f2(tenF);
                tenF(list); return;
        }
    }
})();